<?php

namespace app\supplier\controller\giftproduct;

use app\supplier\controller\Controller;

use app\common\model\plus\discount\DiscountGitProduct;

use app\common\model\product\Product;

/**
 * 店员控制器
 */
class Giftproduct extends Controller
{

    /**
     * 列表
     */
    public function index($search = '')
    {
        // 列表
        $model = new DiscountGitProduct;
        $data = $this->postData();
        $data['shop_supplier_id'] =  $this->getSupplierId();
        $list = $model->getList(10, $search, $data);
        return $this->renderSuccess('', compact('list'));
    }


    public function getProductListData($shop_supplier_id=''){
        $map = [];
        $map[] = ['shop_supplier_id','=',$shop_supplier_id];
        $map[] = ['product_status','=',20];
        $map[] = ['audit_status','=',10];
        $map[] = ['is_delete','=',0];
        $list = (new Product()) -> where($map) -> field('product_id,product_name') -> select();
        return $list;
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isGet()) {
            $list = $this -> getProductListData($this->getSupplierId());

            return $this->renderSuccess('', compact('list'));
        }
        $model = new DiscountGitProduct;
        //传过来的信息
        $data = $this->postData();
        $data['stock'] = 9999;
        $data['audit_status'] = 10;
        $data['shop_supplier_id'] = $this->getSupplierId();
        // 新增记录
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    public function detail($gift_product_id)
    {
        $detail = DiscountGitProduct::detail($gift_product_id);
//        $list = $this -> getProductListData($this->getSupplierId());
        $list = [];
        return $this->renderSuccess('', compact('detail','list'));
    }

    /**
     * 编辑店员
     */
    public function edit($gift_product_id)
    {
        if ($this->request->isGet()) {
            return $this->detail($gift_product_id);
        }
        $model = DiscountGitProduct::detail($gift_product_id);
        //编辑员的数据
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');

    }

    /**
     * 删除
     */
    public function delete($gift_product_id)
    {
        // 店员详情
        $model = DiscountGitProduct::detail($gift_product_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
